﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Xpus.Website.Utilities;
using Xpus.Website.Configuration;

namespace Xpus.Website.Data
{
	partial class User
	{
		#region 字段验证
		/// <summary>
		/// UserName属性长度最大值。
		/// </summary>
		public const int UserNameMaxLength = 256;

		/// <summary>
		/// Nickname属性长度最大值。
		/// </summary>
		public const int NicknameMaxLength = 256;

		/// <summary>
		/// SecurityEmail属性长度最大值。
		/// </summary>
		public const int SecurityEmailMaxLength = 256;

		/// <summary>
		/// PasswordHash属性长度最大值。
		/// </summary>
		public const int PasswordHashMaxLength = 128;

		/// <summary>
		/// PasswordSalt属性长度最大值。
		/// </summary>
		public const int PasswordSaltMaxLength = 128;

		/// <summary>
		/// PasswordQuestion属性长度最大值。
		/// </summary>
		public const int PasswordQuestionMaxLength = 256;

		/// <summary>
		/// PasswordAnswer属性长度最大值。
		/// </summary>
		public const int PasswordAnswerMaxLength = 256;

		/// <summary>
		/// RegisterIP属性长度最大值。
		/// </summary>
		public const int RegisterIPMaxLength = 128;

		/// <summary>
		/// ContactEmail属性长度最大值。
		/// </summary>
		public const int ContactEmailMaxLength = 256;

		/// <summary>
		/// Settings属性长度最大值。
		/// </summary>
		public const int SettingsMaxLength = int.MaxValue;

		partial void OnUserNameChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("UserName", value);
			FieldChecker.CheckFieldTooLong("UserName", value, UserNameMaxLength);
		}

		partial void OnLoweredUserNameChanging(string value)
		{
			OnUserNameChanging(value);
		}

		partial void OnNicknameChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("Nickname", value);
			FieldChecker.CheckFieldTooLong("Nickname", value, NicknameMaxLength);
		}

		partial void OnSecurityEmailChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("SecurityEmail", value);
			FieldChecker.CheckFieldTooLong("SecurityEmail", value, SecurityEmailMaxLength);
		}

		partial void OnLoweredSecurityEmailChanging(string value)
		{
			OnSecurityEmailChanging(value);
		}

		partial void OnPasswordHashChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("PasswordHash", value);
			FieldChecker.CheckFieldTooLong("PasswordHash", value, PasswordHashMaxLength);
		}

		partial void OnPasswordSaltChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("PasswordSalt", value);
			FieldChecker.CheckFieldTooLong("PasswordSalt", value, PasswordSaltMaxLength);
		}

		partial void OnPasswordQuestionChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("PasswordQuestion", value);
			FieldChecker.CheckFieldTooLong("PasswordQuestion", value, PasswordQuestionMaxLength);
		}

		partial void OnPasswordAnswerChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("PasswordAnswer", value);
			FieldChecker.CheckFieldTooLong("PasswordAnswer", value, PasswordAnswerMaxLength);
		}

		partial void OnRegisterIPChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("RegisterIP", value);
			FieldChecker.CheckFieldTooLong("RegisterIP", value, RegisterIPMaxLength);
		}

		partial void OnContactEmailChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("ContactEmail", value);
			FieldChecker.CheckFieldTooLong("ContactEmail", value, ContactEmailMaxLength);
		}

		partial void OnSettingsChanging(string value)
		{
			FieldChecker.CheckFieldNotNull("Settings", value);
			FieldChecker.CheckFieldTooLong("Settings", value, SettingsMaxLength);
		}
		#endregion

		#region 处理Settings属性
		SettingsUtility _theSettings = new SettingsUtility(string.Empty);

		partial void OnCreated()
		{
			this.Settings = string.Empty;
		}

		partial void OnLoaded()
		{
			_theSettings = new SettingsUtility(this.Settings);
			LoadSettings();
		}

		/// <summary>
		/// 从Settings字段中读取设置。
		/// </summary>
		internal void LoadSettings()
		{
		}

		/// <summary>
		/// 将设置保存到Settings字段中。
		/// </summary>
		internal void SaveSettings()
		{
			this.Settings = _theSettings.ToString();
		}
		#endregion

		/// <summary>
		/// 检查指定用户名的用户是否存在。
		/// </summary>
		/// <param name="userName">指定的用户名。</param>
		/// <returns>该用户名是否存在。</returns>
		public static bool IsUserNameExists(string userName)
		{
			XpusDataContext dc = new XpusDataContext(XpusConfigurationSection.Instance.Database.ConnectionString);

			string un = userName.ToLower();
			var q = from u in dc.Users
					where u.LoweredUserName == un
					select u.UserName;

			return q.Count() > 0;
		}

		/// <summary>
		/// 检验指定安全邮箱的用户是否存在。
		/// </summary>
		/// <param name="securityEmail">指定的安全邮箱。</param>
		/// <returns>该安全邮箱是否存在。</returns>
		public static bool IsSecurityEmailExists(string securityEmail)
		{
			XpusDataContext dc = new XpusDataContext(XpusConfigurationSection.Instance.Database.ConnectionString);

			string se = securityEmail.ToLower();
			var q = from u in dc.Users
					where u.LoweredSecurityEmail == se
					select u.SecurityEmail;

			return q.Count() > 0;
		}

		/// <summary>
		/// 通过用户名获取用户。
		/// </summary>
		/// <param name="userName">用户名。</param>
		/// <returns>该用户名指定的用户。如果未找到用户，返回null。</returns>
		public static User GetByUserName(string userName)
		{
			using(XpusDataContext dc = new XpusDataContext(XpusConfigurationSection.Instance.Database.ConnectionString))
			{
				string un = userName.ToLower();
				var q = from u in dc.Users
						where u.LoweredUserName == un
						select u;

				try
				{
					return q.First();
				}
				catch
				{
					return null;
				}
			}
		}

		/// <summary>
		/// 通过安全邮箱获取用户。
		/// </summary>
		/// <param name="securityEmail">安全邮箱。</param>
		/// <returns>该安全邮箱指定的用户。如果未找到用户，返回null。</returns>
		public static User GetBySecurityEmail(string securityEmail)
		{
			using(XpusDataContext dc = new XpusDataContext(XpusConfigurationSection.Instance.Database.ConnectionString))
			{
				string se = securityEmail.ToLower();
				var q = from u in dc.Users
						where u.LoweredSecurityEmail == se
						select u;

				try
				{
					return q.First();
				}
				catch
				{
					return null;
				}
			}
		}
	}
}
